Skip to content

fix(diff): read the index line after a dissimilarity index - #2353

Merged
Byron merged 1 commit into
gitpython-developers:mainfrom
JROtto5:fix/diff-dissimilarity-index
Nov 23, 2026
Merged

Byron merged 1 commit into
gitpython-developers:mainfrom
JROtto5:fix/diff-dissimilarity-index

Conversation

@JROtto5

@JROtto5 JROtto5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I am a Claude (Opus 5.5) agent working through the contributor's account.

Continuing the comparison of GitPython's parsers with what a current Git (2.56) prints, this time the patch header in Diff._index_from_patch_format. With B=True (git diff -B), a file that Git reports as rewritten came back with a_blob and b_blob set to None, and with no modes either when the mode had not changed. The same diff without -B has them. Other files in a -B diff are not affected.

For a broken pair Git writes dissimilarity index N% between the mode lines and the index line. re_header has no group for that line, and every group after it is optional, so the match ends there and the index line is never read. The hunks are still parsed, so nothing warns.

Accept the line, in the place Git writes it, before the index line.

Validation on Linux, CPython 3.14.4, Git 2.56.0:

  • The new fixture is real git diff -B --full-index output for two rewritten files, one with a mode change and one without. test_diff_with_dissimilarity_index fails on the original diff.py (a_blob is None) and passes with the change.
  • commit.diff(None, create_patch=True, B=True) on a real repository with the same two files: before, both had no blobs and the unchanged-mode file had no modes; after, both have the right blob ids and modes.
  • Complete test suite with the default backend: 2302 passed, 60 skipped, 1 xfailed. test/test_diff.py also passes (51) with GIT_PYTHON_BACKEND=gix.
  • Ruff check and format pass.

Not run: mypy, the complete suite with the gix backend, other platforms and Python versions.

With `-B`, Git writes `dissimilarity index N%` for a rewritten file, between
the mode lines and the `index` line. `re_header` has no group for it, so the
match stopped there: for a rewritten file `a_blob` and `b_blob` came back as
`None`, and so did `a_mode` and `b_mode` when the mode had not changed (Git
then puts it on the `index` line). The hunks were still read. Other files in
the same `-B` diff were not affected.

Accept the line, in the place Git writes it, before the `index` line.

The new fixture is real `git diff -B --full-index` output for two rewritten
files, one with a mode change and one without.
`test_diff_with_dissimilarity_index` fails without this change.

Agent-Model: claude-opus-5-5
@Byron
Byron merged commit 029964a into gitpython-developers:main Nov 23, 2026
64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants